<?php

namespace app\common\service\CountryService;

use app\model\BbcModel\Country;
use app\Normal;
use think\annotation\Route;

class CountryService
{
    public function CountryLists($param){
        $page = (isset($param['page']) && $param['page'] != '') ? $param['page'] : 1;
        $limit = (isset($param['limit']) && $param['limit'] != '') ? $param['limit'] : 20;
        if(isset($param['name']) && $param['name'] != ''){
            $data_lists = Country::instance()->where(['df'=>0])->where('name|short_name','like',"%{$param['name']}%")->field('id,icon,bgcolor_code,country_logo,name,short_name,is_show,country_sort')->order('country_sort desc,id desc')->page((int)$page)->limit((int)$limit)->select();
            $total = Country::instance()->where(['df'=>0])->where('name|short_name','like',"%{$param['name']}%")->count();
        }else{
            $data_lists = Country::instance()->where(['df'=>0])->field('id,icon,bgcolor_code,country_logo,name,short_name,is_show,country_sort')->order('country_sort desc,id desc')->page((int)$page)->limit((int)$limit)->select();
            $total = Country::instance()->where(['df'=>0])->count();
        }

        return Normal::returnCode(Normal::SUC,'获取成功',['data_lists'=>$data_lists,'total'=>$total]);
    }

    public function CountryDetails($param){
        if(!isset($param['id']) || !$param['id']){
            return Normal::returnCode(Normal::FAIL,'国家ID不能为空');
        }
        $info = Country::instance()->info(['id'=>$param['id'],'df'=>0,'_fields_'=>'id,icon,bgcolor_code,country_logo,name,short_name,is_show,country_sort']);
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'国家数据不存在');
        }
        return Normal::returnCode(Normal::SUC,'获取成功',$info);
    }

    public function editCountry($param){
        if(!isset($param['id']) || !$param['id']){
            return Normal::returnCode(Normal::FAIL,'国家ID不能为空');
        }
        $info = Country::instance()->info(['id'=>$param['id'],'df'=>0]);
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'国家数据不存在');
        }
        $updateData['icon']         = $param['icon'] ?? '';
        $updateData['bgcolor_code'] = $param['bgcolor_code'] ?? '';;
        $updateData['country_logo'] = $param['country_logo'] ?? '';;
        $updateData['name']         = $param['name'] ?? '';;
        $updateData['short_name']   = $param['short_name'] ?? '';;
        $updateData['is_show']      = $param['is_show'] ?? '';;
        $updateData['country_sort'] = $param['country_sort'] ?? '';;
        $res = Country::instance()->edit(['id'=>$info['id']],$updateData);
        if($res == false){
            return Normal::returnCode(Normal::FAIL,'修改失败');
        }
        return Normal::returnCode(Normal::SUC,'修改成功');
    }
}